package com.neusoft.neuiotms.dcam.fixed.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.model.Asset;
import com.neusoft.neuiotms.dcam.fixed.model.QAsset;
import com.neusoft.neuiotms.dcam.fixed.repository.CustomizedAssetRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedAssetRepositoryImpl extends AbstractRepository implements CustomizedAssetRepository {

    @Override
    public List<Asset> search(final Searchable searchable) {
        final QAsset qAsset = QAsset.asset;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Asset> query = this.query().selectFrom(qAsset);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<Asset> search(final Pageable pageable, final Searchable searchable) {
        final QAsset qAsset = QAsset.asset;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Asset> query = this.query().selectFrom(qAsset);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    public long count(final Searchable searchable) {
        final QAsset qAsset = QAsset.asset;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Asset> query = this.query().selectFrom(qAsset);
        query.where(where);
        return query.fetchCount();
    }

    @Override
    protected Class<?> getModelClass() {
        return Asset.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QAsset qAsset = QAsset.asset;

        // 名称检索
        where.and(super.containsStr(qAsset.name, searchable, "name"));
        // 原始资产编码
        where.and(super.containsStr(qAsset.originalCode, searchable, "originalCode"));
        // 编码检索
        where.and(super.containsStr(qAsset.code, searchable, "code"));
        if (searchable.hasKey("codes") && (searchable.getStrArray("codes").length > 0)) {
            where.and(super.inStrings(qAsset.code, searchable, "codes"));
        }
        //去除已选的设备
        if (searchable.hasKey("notExitsCodes") && (searchable.getStrArray("notExitsCodes").length > 0)) {
            where.andNot(super.inStrings(qAsset.code, searchable, "notExitsCodes"));
        }
        //        where.andNot(right)
        // 分类查询 大中小
        where.and(super.equalsStr(qAsset.bigTypeCode, searchable, "bigTypeCode"));
        where.and(super.equalsStr(qAsset.middleTypeCode, searchable, "middleTypeCode"));
        where.and(super.equalsStr(qAsset.smallTypeCode, searchable, "smallTypeCode"));
        if (searchable.hasKey("smallTypeCodes") && (null != searchable.getStrArray("smallTypeCodes"))
                && (searchable.getStrArray("smallTypeCodes").length > 0)) {
            where.and(super.inStrings(qAsset.smallTypeCode, searchable, "smallTypeCodes"));

        }
        if (searchable.hasKey("typeCodesArr") && (null != searchable.getStrArray("typeCodesArr"))
                && (searchable.getStrArray("typeCodesArr").length > 0)) {
            where.and(super.inStrings(qAsset.bigTypeCode, searchable, "typeCodesArr"));
            where.or(super.inStrings(qAsset.middleTypeCode, searchable, "typeCodesArr"));
            where.or(super.inStrings(qAsset.smallTypeCode, searchable, "typeCodesArr"));

        }
        // 管理部门编码
        if (searchable.hasKey("managementOrgCode")) {
            where.and(super.inStrings(qAsset.managementOrgCode, searchable, "managementOrgCode"));
        }
        //        // 操作员所属部门
        //        if (searchable.hasKey("operaterOrg") && (searchable.getStrArray("operaterOrg").length > 0)) {
        //            where.or(super.inStrings(qAsset.managementOrgCode, searchable, "operaterOrg"));
        //        }
        // 责任人编码
        where.and(super.equalsStr(qAsset.managementUserCode, searchable, "managementUserCode"));
        // 责任人姓名
        where.and(super.containsStr(qAsset.managementUserName, searchable, "managementUserName"));
        where.and(super.equalsStr(qAsset.specifications, searchable, "specifications"));
        // 所属
        where.and(super.equalsStr(qAsset.belongs, searchable, "belongs"));
        // 机房
        //        where.and(super.equalsStr(qAsset.location, searchable, "location"));
        // 类别
        where.and(super.equalsStr(qAsset.categories, searchable, "categories"));
        if (searchable.hasKey("categoriesArr") && (searchable.getStrArray("categoriesArr").length > 0)) {
            where.and(super.inStrings(qAsset.categories, searchable, "categoriesArr"));
        }
        // 用途
        where.and(super.containsStr(qAsset.purpose, searchable, "purpose"));
        //位置
        where.and(super.equalsStr(qAsset.location, searchable, "location"));
        if (searchable.hasKey("locationArr") && (searchable.getStrArray("locationArr").length > 0)) {
            where.and(super.inStrings(qAsset.location, searchable, "locationArr"));
        }
        // 合同
        where.and(super.equalsStr(qAsset.contractNo, searchable, "contractNo"));
        // 金额
        if (!StringUtils.isEmpty(searchable.getStrValue("money"))) {
            where.and(super.operateDouble(qAsset.money, searchable, "money"));
        }

        // 数量
        if (!StringUtils.isEmpty(searchable.getStrValue("amount"))) {
            where.and(super.operateInt(qAsset.amount, searchable, "amount"));
        }

        if (searchable.hasKey("assetTypeCodes") && (searchable.getStrArray("assetTypeCodes").length > 0)) {
            where.and(super.inStrings(qAsset.smallTypeCode, searchable, "assetTypeCodes"));
        }
        // 维保时间
        if (!StringUtils.isEmpty(searchable.getStrValue("maintenanceStartBegin"))) {
            where.and(
                    super.afterOrEqualsDate(qAsset.maintenanceStartTime, searchable,
                            "maintenanceStartBegin"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("maintenanceStartEnd"))) {
            where.and(super.beforeOrEqualsDate(qAsset.maintenanceStartTime, searchable,
                    "maintenanceStartEnd"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("maintenanceEndBegin"))) {
            where.and(super.afterOrEqualsDate(qAsset.maintenanceEndTime, searchable,
                    "maintenanceEndBegin"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("maintenanceEndEnd"))) {
            where.and(super.beforeOrEqualsDate(qAsset.maintenanceEndTime, searchable,
                    "maintenanceEndEnd"));
        }

        // 购置时间
        if (!StringUtils.isEmpty(searchable.getStrValue("purchaseDateBegin"))) {
            where.and(
                    super.afterOrEqualsDate(qAsset.purchaseDate, searchable,
                            "purchaseDateBegin"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("purchaseDateEnd"))) {
            where.and(super.beforeOrEqualsDate(qAsset.purchaseDate, searchable,
                    "purchaseDateEnd"));
        }
        // 枷锁状态
        where.and(super.equalsBool(qAsset.locked, searchable, "locked"));
        // 资产状态
        where.and(super.equalsStr(qAsset.status, searchable, "status"));
        if (searchable.hasKey("statusCode") && (searchable.getStrArray("statusCode").length > 0)) {
            where.and(super.inStrings(qAsset.status, searchable, "statusCode"));
        }
        if (searchable.hasKey("managementOrgCodeIsnull")) {
            if (searchable.getBooleanValue("managementOrgCodeIsnull")) {
                where.and(qAsset.managementOrgCode.isNull());
            } else {
                where.and(qAsset.managementOrgCode.isNotNull());
            }
        }

        // 定制导出记录的唯一编号
        if (searchable.hasKey("id") && (searchable.getStrArray("id").length > 0)) {
            where.and(super.inLongs(qAsset.id, searchable, "id"));
        }

        // 标签打印
        if (searchable.hasKey("labelPrinted")) {
            final Boolean labelPrinted = searchable.getBooleanValue("labelPrinted");
            if (labelPrinted) {
                where.and(qAsset.labelPrinted.eq(true));
            } else {
                final BooleanBuilder bb = new BooleanBuilder();
                bb.or(qAsset.labelPrinted.eq(false));
                bb.or(qAsset.labelPrinted.isNull());
                where.and(bb);
            }
        }

        // 全局模糊查询
        if (!StringUtils.isEmpty(searchable.getStrValue("globalSearch"))) {
            final BooleanBuilder whereGlobal = new BooleanBuilder();

            /** 资产名称 */
            whereGlobal.or(super.containsStr(qAsset.name, searchable, "globalSearch"));
            /** 资产编号 */
            whereGlobal.or(super.containsStr(qAsset.code, searchable, "globalSearch"));
            /** 原始资产编号 */
            whereGlobal.or(super.containsStr(qAsset.originalCode, searchable, "globalSearch"));
            /** 供应商名称 */
            whereGlobal.or(super.containsStr(qAsset.supplierName, searchable, "globalSearch"));
            /** 规格型号 */
            whereGlobal.or(super.containsStr(qAsset.specifications, searchable, "globalSearch"));
            /** 序列号 */
            whereGlobal.or(super.containsStr(qAsset.serialnumber, searchable, "globalSearch"));
            /** 机房 */
            whereGlobal.or(super.containsStr(qAsset.computerRoom, searchable, "globalSearch"));
            /** 位置名称 */
            whereGlobal.or(super.containsStr(qAsset.locationName, searchable, "globalSearch"));
            /** 机柜 */
            whereGlobal.or(super.containsStr(qAsset.cabinet, searchable, "globalSearch"));
            /** 位置 */
            whereGlobal.or(super.containsStr(qAsset.locationName, searchable, "globalSearch"));
            /** 品牌 */
            whereGlobal.or(super.containsStr(qAsset.brand, searchable, "globalSearch"));
            /** 大类型名称 */
            whereGlobal.or(super.containsStr(qAsset.bigTypeName, searchable, "globalSearch"));
            /** 大类型编码 */
            whereGlobal.or(super.containsStr(qAsset.bigTypeCode, searchable, "globalSearch"));
            /** 中类型名称 */
            whereGlobal.or(super.containsStr(qAsset.middleTypeName, searchable, "globalSearch"));
            /** 中类型编码 */
            whereGlobal.or(super.containsStr(qAsset.middleTypeCode, searchable, "globalSearch"));
            /** 小类型名称 */
            whereGlobal.or(super.containsStr(qAsset.smallTypeName, searchable, "globalSearch"));
            /** 小类型编码 */
            whereGlobal.or(super.containsStr(qAsset.smallTypeCode, searchable, "globalSearch"));
            /** 合同号 */
            whereGlobal.or(super.containsStr(qAsset.contractNo, searchable, "globalSearch"));
            /** 合同名称 */
            whereGlobal.or(super.containsStr(qAsset.contractName, searchable, "globalSearch"));
            /** 计量单位 */
            whereGlobal.or(super.containsStr(qAsset.measureUnit, searchable, "globalSearch"));
            /** 使用期限 */
            whereGlobal.or(super.containsStr(qAsset.usePeriod, searchable, "globalSearch"));
            //            /** 经办人 */
            //            whereGlobal.or(super.containsStr(qAsset.agent, searchable, "globalSearch"));
            /** 备注 */
            whereGlobal.or(super.containsStr(qAsset.note, searchable, "globalSearch"));
            /** 管理部门名称 */
            whereGlobal.or(super.containsStr(qAsset.managementOrgName, searchable, "globalSearch"));
            /** 管理部门编码 */
            whereGlobal.or(super.containsStr(qAsset.managementOrgCode, searchable, "globalSearch"));
            /** 责任人名称 */
            whereGlobal.or(super.containsStr(qAsset.managementUserName, searchable, "globalSearch"));
            /** 责任人编码 */
            whereGlobal.or(super.containsStr(qAsset.managementUserCode, searchable, "globalSearch"));

            where.and(whereGlobal);

        }
        return where;
    }

}
